var userIdmessage =  document.getElementById("userIdmessage")
var useId=userIdmessage.innerHTML;
// 封装获取元素的方法
function getType(selector){
    return selector.substring(0,1)=='.'?document.getElementsByClassName(selector.substring(1)):document.getElementById(selector.substring(1));
}
var test = window.location.href;
var numberId=test.split('?')[1]

// 要把videoId改成numberId
// 开始交互
// showComment();

// function showLike(){
//     axios({
//         url: '/video/JudgeThumbsUp',
//         method: 'post',
//         params:{
//             userId:1,
//             videoId:98
//         }
//     })
//     .then(function(data){
//         if(data.data.data.object1==1){
//             getType('.iconfont icon-dianzan')[0].className='iconfont icon-dianzan_kuai'; 
//             getType('.play-span')[1].style.color='red';    
//         } 
//         getType('.play-span')[1].onclick=function(){
//                 axios({
//                     url: '/video/ThumbsUp',
//                     method: 'post',
//                     params:{
//                         userId:1,
//                         videoId:98
//                     }
//                 })
//                 .then(function(data){
//                     if(data.data.data.object1==1){
//                         getType('.iconfont icon-dianzan')[0].className='iconfont icon-dianzan_kuai'; 
//                         getType('.play-span')[1].style.color='red'; 
//                     }
//                     else{
//                         getType('.iconfont icon-dianzan_kuai')[0].className='iconfont icon-dianzan';
//                         getType('.play-span')[1].style.color='hsla(0,0%,100%,.5)';
//                     }
//                     thumbs();
//                 })
//                 .catch(function(error){
//                     console.log('错误')
//                 })         
//         }
//     })
//     .catch(function(error){
//         console.log('错误')
//     })    
// }

// 判断是否点赞视频
function showLike(){
    axios({
        url: '/video/JudgeThumbsUp',
        method: 'post',
        params:{
            userId:useId,
            videoId:numberId
        }
    })
    .then(function(data){
        if(data.data.data.object1==1){
            getType('.iconfont icon-dianzan')[0].className='iconfont icon-dianzan_kuai'; 
            getType('.play-span')[1].style.color='red';    
        } 
        getType('.play-span')[1].onclick=function(){
            axios({
                url: '/video/ThumbsUp',
                method: 'post',
                params:{
                    userId:useId,
                    videoId:numberId
                }
            })
            .then(function(data){
                if(data.data.data.object1==1){
                    getType('.iconfont icon-dianzan')[0].className='iconfont icon-dianzan_kuai'; 
                    getType('.play-span')[1].style.color='red'; 
                }
                else{
                    getType('.iconfont icon-dianzan_kuai')[0].className='iconfont icon-dianzan';
                    getType('.play-span')[1].style.color='hsla(0,0%,100%,.5)';
                }
                getType('.praiseNum')[0].innerHTML=data.data.data.object

            })
            .catch(function(error){
                console.log('错误')
            }) 
        }
    })
    .catch(function(error){
        console.log('错误')
    })    

}
if (userIdmessage.innerHTML == '') {
    getType('.play-span')[1].onclick = function () {
        getType('#loginmodal').style.display = 'block';
    }
} 
else {
    showLike()
}
// } 




// 进入页面之后知道他的点赞收藏
function thumbs(){
    axios({
        url: '/video/tv/findTVMsgByTVId',
        method: 'get',
        params:{
            tvId:numberId
        }
    })
    .then(function(data){
        console.log(data.data.data)
        // // 收藏数
        getType('.collectNum')[0].innerHTML=data.data.data.collections
        // //  点赞数
        getType('.praiseNum')[0].innerHTML=data.data.data.thumbsUp
    })
    .catch(function(error){
        console.log('错误')
    })
}
thumbs();

//3.判断是否登录并对这个视频进行评论
if(useId==''){
    getType('.carry-login')[0].onclick=function(){
        // 弹窗就会出现
        getType('#loginmodal').style.display='block';
    }
}else{
    getType('.login-success')[0].style.display='block';
    getType('.no-login')[0].style.display='none'
    getType('.footer-button')[0].onclick=function(){
        if(getType('#text1').value==''){ 
            getType('.import-input')[0].style.display='block';
            getType('.import')[0].style.opacity='1'
            setTimeout(function(){
                getType('.import-input')[0].style.display='none';
                getType('.import')[0].style.opacity='0'
            },3000)
        }else{  
            axios({
                url: '/video/addComment',
                method: 'post',
                params:{
                    content:getType('#text1').value,
                    userId:useId,
                    videoId:numberId
                }
            })
            .then(function(data){
                getType('#text1').value==" "
                getType('.import-input1')[0].style.display='block';
                getType('.import1')[0].style.opacity='1'
                setTimeout(function(){
                    getType('.import-input1')[0].style.display='none';
                    getType('.import1')[0].style.opacity='0'
                },3000)
                // 进行遍历主评论
                // showComment();
                window.location.reload();
            })
            
            .catch(function(error){
                console.log('错误')
            })
        }
        
    }

}







//  4.遍历热度
 function showDoh(){
    axios({
        url: '/video/Doh',
        method: 'get',
        params:{
            userId: useId,
            videoId:115
        }
    })
    .then(function(data){
        getType('.desc-i')[0].innerHTML=`<i class='iconfont icon-redu'></i>${data.data.data}`
    })
    .catch(function(error){
        console.log('错误')
    })      
}
// showDoh()






// 7.点击出现内容简介
var want = document.getElementsByClassName('iconfont icon-xiangxia')[0]
var green=0;
getType('.tags-test')[0].onclick=function(){
    if(green == 0){
        getType('.mod_row_box')[0].style.display='block';
        want.className='iconfont icon-xiangshangshouqi'     
        green=1;

    }else{
        getType('.mod_row_box')[0].style.display='none';
        getType('.icon-xiangshangshouqi')[0].className='iconfont icon-xiangxia'
        green=0;
    }
}

//8.点击进行滑动
var rightBtn=getType('.qy-right')[0]
var leftBtn=getType('.qy-left')[0]
rightBtn.onclick = function(){
    getType('.trochanter-ul')[0].style.transform = "translateX(-100%)";
    getType('.trochanter-ul')[0].style.transition = "all 1s";
}
leftBtn.onclick = function(){
    getType('.trochanter-ul')[0].style.transform = "translateX(0)";
    getType('.trochanter-ul')[0].style.transition = "all 1s";
}

var rightBtn1=getType('.qy-right')[1]
var leftBtn1=getType('.qy-left')[1]
rightBtn1.onclick = function(){
    getType('.four-li2')[0].style.transform = "translateX(-100%)";
    getType('.four-li2')[0].style.transition = "all 1s";
}
leftBtn1.onclick = function(){
    getType('.four-li2')[0].style.transform = "translateX(0)";
    getType('.four-li2')[0].style.transition = "all 1s";
}

// 收藏
function flagFun(e){
    e=e||event;
    console.log(e.target.checked);
    if(e.target.checked){
        e.target.setAttribute("checked","false")
    }
}
// 进入页面看他是否收藏
function showCollect(){
    axios({
        url: '/video/JudgeCollection',
        method: 'post',
        params:{
            userId:useId,
            videoId:numberId
        }
    })
    .then(function(data){
        var name1=[];
        if(data.data.data.object1==null){
            getType('.play-span')[0].style.color='#aaa';
        }else{
            getType('.iconfont icon-shoucang')[0].className='iconfont icon-shoucang1'; 
            getType('.play-span')[0].style.color='red'; 
            for(var i=0;i<data.data.data.object1.length;i++){
                name1.push(data.data.data.object1[i].collectionFoldersName)
            }   
        }
        getType('.play-span')[0].onclick=function(){
                getType('.shade')[0].style.display='block';
                getType('.CPM')[0].style.opacity='1';
                getType('.closew')[0].onclick=function(){
                    getType('.shade')[0].style.display='none';
                    getType('.CPM')[0].style.opacity='0';
                }
                //遍历出来所有收藏夹
                function allCollections(){
                    axios({
                        url: '/video/findAllCollectionFolders',
                        method: 'get',
                        params:{
                            userId:useId,
                        }
                    })
                    .then(function(data){
                        let flags=1;
                        let str3='';
                        for(let i=0;i<data.data.data.length;i++){  
                            if(name1.indexOf(data.data.data[i].collectionFoldersName)!=-1){
                            str3+=`
                                <li>
                                    <label for="选择框">
                                        <input type="checkbox" id="${i}" onclick="flagFun()" checked="checked" name="radio">
                                        <span class='fav-title'>${data.data.data[i].collectionFoldersName}</span>
                                    </label>
                                </li>
                            `
                            }else{
                                str3+=`
                                    <li>
                                        <label for="选择框">
                                            <input type="checkbox" id="${i}" onclick="flagFun()"   name="radio">
                                            <span class='fav-title'>${data.data.data[i].collectionFoldersName}</span>
                                        </label>
                                    </li>
                                `
                            }
                            var colId=data.data.data
                            // 被选中的状态
                            // $("input[name='radio'][value='"+李典+"']").prop("checked", "checked");
                            getType('.btn-sure')[0].onclick=function(){
                                if($('input[name="radio"]:checked').val()==undefined){
                                    alert('请选择收藏夹')
                                }else{
                                    var temp =document.getElementsByName("radio");
                                    for(let i=0;i<temp.length;i++){
                                        if(temp[i].checked){
                                            axios({
                                                url: '/video/Collection',
                                                method: 'post',
                                                params:{
                                                    userId:useId,
                                                    videoId:numberId,
                                                    collectionFoldersId:colId[i].collectionFoldersId
                                                }
                                            })
                                            .then(function(data){
                                                window.location.reload();
                                            })
                                            .catch(function(error){
                                                console.log('错误')
                                            })
                                        }
                                    }
                                }
                            } 
                        }
                        getType('.shadeUl')[0].innerHTML=str3;
                    //    shadeUl.appendChild(str3)
        
                    })
                    .catch(function(error){
                        console.log('错误')
                    })
                }
                allCollections()
                
                // 点击新建一个收藏夹
                // 点击新建会变
                        getType('.add-group')[0].onclick=function(e){
                            e=e||event;
                            e.cancelBubble = true;
                            getType('.add-all')[0].style.display='none'
                            getType('.group-input')[0].style.display='block'
                            // 开始创键新的文件夹名称
                            getType('.group-submit')[0].onclick=function(){
                                if(getType('.form-input')[0].value==''){
                                    getType('.alert')[0].style.display='block'
                                    setTimeout(function(){
                                        getType('.alert')[0].style.display='none'
                                    },3000)
                                }else{
                                    // for(let i=0;i<getType('.fav-title').length;i++){
                                    //     if(getType('.form-input')[0].value==getType('.fav-title')[i].innerHTML){
                                    //         alert('收藏夹名字重复了')
                                    //     }else{
                                        
                                    //     }
                                    // }
                                // 添加新建的收藏夹名字
                                axios({
                                    url: '/video/addCollectionFolder',
                                    method: 'post',
                                    params:{
                                        userId:useId,
                                        collectionFoldersName:getType('.form-input')[0].value
                                    }
                                })
                                .then(function(data){
                                    allCollections()
                                    getType('.form-input')[0].value=''
                                })
                                .catch(function(error){
                                    console.log('错误')
                                })
                                }
                                return false;
                            }
                        }
                        document.onclick = function(){
                            getType('.add-all')[0].style.display='block'
                            getType('.group-input')[0].style.display='none'
                        }        
        }
       
    })
    .catch(function(error){
        console.log('错误')
    })  
}
if(userIdmessage.innerHTML==''){
    getType('.play-span')[0].onclick=function(){
        getType('#loginmodal').style.display = 'block';
    }
}else{
    showCollect();
    // 显示收藏加数
    thumbs()
}




// 10.发送弹幕
getType('.play-span')[4].style.color='#00cc4c';
var colors = ["#369","#16481f","orange","red","skyblue","gray","#aaa","lightgreen","green","darkblue","#06257b","darkred","#e0a111","black", "#e2128f", "#860dad", "#94ad0d", "#ce3131"]; //用来设置的颜色
getType('.forward')[0].onclick=function(){
    // 获取输入框的值
    var text = getType("#text").value;
    // 创建元素
    var p = document.createElement("p")
    // 设置p标签的内容
    p.innerText = text;
    //给p标签添加msg类
    p.setAttribute("class", "msg")
    // 设置弹幕的随机高度
    var h = Math.random() * 120;
    p.style.top = h + "px"
    p.style.color = colors[parseInt(Math.random()*colors.length)]; 
    // 把弹幕添加到页面上
    getType('.play-flash')[0].appendChild(p)
    // 设置输入框为空
    document.getElementById("text").value = ""
  }
  var msgs=getType('.msg')
  getType('.play-span')[5].onclick=function(){
    getType('.play-span')[4].style.color='hsla(0,0%,100%,.5)';
    getType('.play-span')[5].style.color='#00cc4c';

    for(let i=0;i<msgs.length;i++){
        getType('.msg')[i].innerHTML='';
    }
    
  }












